﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ToyChat.BuiltinModules
{
    /// <summary>
    /// This dialog can be used to get a <see cref="ChannelControlMessage"/> for various channel operations (Create / Modify / Delete, etc.?)
    /// </summary>
    public partial class CreateChannelDialog : UserControl, IDialog
    {
        private ChannelControlMessage msg = new ChannelControlMessage();
        private FlagsSelector<ChannelPermissionFlags> selector = new FlagsSelector<ChannelPermissionFlags>();

        /// <summary>
        /// Initializes a new (empty) instance of <see cref="CreateChannelDialog"/>
        /// </summary>
        public CreateChannelDialog()
        {
            InitializeComponent();
            this.permsExpander1.Content = selector;
            selector.Value = ChannelPermissionFlags.Speak;
        }

        /// <summary>
        /// Gets or Sets the <see cref="ChannelControlMessage"/> that this dialog is modifying / displaying
        /// </summary>
        public ChannelControlMessage Message
        {
            get { return this.msg; }
            set
            {
                this.msg = value;
                this.nameBox1.Text = this.msg.ChannelName ?? string.Empty;
                this.pwBox1.Password = this.msg.Password ?? string.Empty;
                this.motdBox1.Text = this.msg.MotD ?? string.Empty;
                this.hiddenCB1.IsChecked = this.msg.IsHidden ?? false;
                this.selector.Value = this.msg.DefaultPermissions ?? ChannelPermissionFlags.Speak;
                this.extended1.Visibility = this.msg.Mode == ControlMode.Delete ? System.Windows.Visibility.Hidden : System.Windows.Visibility.Visible;
            }
        }

        /// <summary>
        /// This event is fired when the user clicks the OK button
        /// </summary>
        public event RoutedEventHandler OK;

        /// <summary>
        /// This event is fired when the user clicks the cancel button
        /// </summary>
        public event RoutedEventHandler Cancel;

        /// <summary>
        /// Called when the OK button is clicked
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The arguments of the event</param>
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            this.msg.ChannelName = this.nameBox1.Text;
            this.msg.Password = this.pwBox1.Password;
            this.msg.MotD = this.motdBox1.Text;
            this.msg.IsHidden = this.hiddenCB1.IsChecked;
            this.msg.DefaultPermissions = this.selector.Value;
            if (this.OK != null)
                this.OK.Invoke(this, e);
        }

        /// <summary>
        /// Called when the Cancel button is clicked
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The arguments of the event</param>
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.msg = new ChannelControlMessage();
            if (this.Cancel != null)
                this.Cancel.Invoke(this, e);
        }

        /// <summary>
        /// Gets the name input box of this <see cref="CreateChannelDialog"/>
        /// </summary>
        public IInputElement MainInput
        {
            get { return this.nameBox1; }
        }
    }
}
